﻿using System.Windows;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using HP.Cachalote.WPF.Model;
using HP.Cachalote.WPF.Shared;

namespace HP.Cachalote.WPF.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class SummaryMessageDisplayViewModel : MyViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the SummaryMessageDisplayViewModel class.
        /// </summary>
        public SummaryMessageDisplayViewModel(IDataService dataService) : base(dataService) { }

        /// <summary>
        /// Register all necessary messages. It is called by constructor in base class.
        /// </summary>
        protected override void RegisterMessages()
        {
            base.RegisterMessages();

            Messenger.Default.Register<string>(this, MyViewModelBase.RequestDisplayErrorMessageToken, (msg) =>
            {
                SetupErrorMessage(msg);
            });

            Messenger.Default.Register<string>(this, MyViewModelBase.RequestClearErrorMessageToken, (msg) =>
            {
                SetupErrorMessage(msg);
            });

            Messenger.Default.Register<string>(this, MyViewModelBase.RequestDisplayMessageToken, (msg) =>
            {
                SetupSummaryrMessage(msg);
            });

            Messenger.Default.Register<string>(this, MyViewModelBase.RequestClearMessageToken, (msg) =>
            {
                SetupSummaryrMessage(msg);
            });
        }

        /// <summary>
        /// The <see cref="ErrorMessage" /> property's name.
        /// </summary>
        public const string ErrorMessagePropertyName = "ErrorMessage";

        private string _errorMessage = string.Empty;

        /// <summary>
        /// Sets and gets the ErrorMessage property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public string ErrorMessage
        {
            get
            {
                return _errorMessage;
            }

            set
            {
                if (_errorMessage == value)
                {
                    return;
                }

                RaisePropertyChanging(ErrorMessagePropertyName);
                _errorMessage = value;
                RaisePropertyChanged(ErrorMessagePropertyName);
            }
        }

        /// <summary>
        /// The <see cref="ErrorMessageVisibility" /> property's name.
        /// </summary>
        public const string ErrorMessageVisibilityPropertyName = "ErrorMessageVisibility";

        private Visibility _errorMessageVisibility = Visibility.Collapsed;

        /// <summary>
        /// Sets and gets the ErrorMessageVisibility property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public Visibility ErrorMessageVisibility
        {
            get
            {
                return _errorMessageVisibility;
            }

            set
            {
                if (_errorMessageVisibility == value)
                {
                    return;
                }

                RaisePropertyChanging(ErrorMessageVisibilityPropertyName);
                _errorMessageVisibility = value;
                RaisePropertyChanged(ErrorMessageVisibilityPropertyName);
            }
        }

        /// <summary>
        /// The <see cref="SummaryMessage" /> property's name.
        /// </summary>
        public const string SummaryMessagePropertyName = "SummaryMessage";

        private string _summaryMessage = string.Empty;

        /// <summary>
        /// Sets and gets the SummaryMessage property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public string SummaryMessage
        {
            get
            {
                return _summaryMessage;
            }

            set
            {
                if (_summaryMessage == value)
                {
                    return;
                }

                RaisePropertyChanging(SummaryMessagePropertyName);
                _summaryMessage = value;
                RaisePropertyChanged(SummaryMessagePropertyName);
            }
        }

        /// <summary>
        /// The <see cref="SummaryMessageVisibility" /> property's name.
        /// </summary>
        public const string SummaryMessageVisibilityPropertyName = "SummaryMessageVisibility";

        private Visibility _summaryMessageVisibility = Visibility.Collapsed;

        /// <summary>
        /// Sets and gets the SummaryMessageVisibility property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public Visibility SummaryMessageVisibility
        {
            get
            {
                return _summaryMessageVisibility;
            }

            set
            {
                if (_summaryMessageVisibility == value)
                {
                    return;
                }

                RaisePropertyChanging(SummaryMessageVisibilityPropertyName);
                _summaryMessageVisibility = value;
                RaisePropertyChanged(SummaryMessageVisibilityPropertyName);
            }
        }

        private void SetupErrorMessage(string msg)
        {
            ErrorMessage = msg;
            ErrorMessageVisibility = string.IsNullOrEmpty(msg) ? Visibility.Collapsed : Visibility.Visible;
        }

        private void SetupSummaryrMessage(string msg)
        {
            SummaryMessage = msg;
            SummaryMessageVisibility = string.IsNullOrEmpty(msg) ? Visibility.Collapsed : Visibility.Visible;
        }
    }
}